﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ResourceRequest.Services;
using ePlanet.ResourceRequest.Business;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.DataAccess;
using Microsoft.Practices.Unity;

namespace Web.Views.TempUserManage
{
    public partial class TempUserView : PageBase
    {
        #region 页面变量
        [Dependency]
        public ITempUserService m_TempUserService
        {
            get;
            set;
        }

        private string m_UserID = string.Empty;

        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Request.QueryString["TempUserID"] == null))
            {
                m_UserID = Request.QueryString["TempUserID"];
            }
            if (!IsPostBack)
            {
                this.BindTempUser();
            }
        }

        #region "按钮事件"
        protected void btnAgree_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.AgreeLogin())
                {
                    string url = "TempUserManager.aspx?StatusID=" + ((int)TempUserStausEnum.CanLogin).ToString();
                    this.Jump("设置允许用户登录成功", url);
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void btnDisAgree_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.DisAgreeLogin())
                {
                    string url = "TempUserManager.aspx?StatusID=" + ((int)TempUserStausEnum.CannotLogin).ToString();
                    this.Jump("设置不允许用户登录成功", url);
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void btnReSetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                this.ResetPassword();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }
        #endregion

        #region 私有方法
        private void BindTempUser()
        {
            if (string.IsNullOrEmpty(this.m_UserID)) return;
            TempUser oTempUser = this.m_TempUserService.GetTempUserByUserID(m_UserID);
            if (oTempUser != null)
            {
                TempUser.TEMPUSERRow row = oTempUser.TEMPUSER[0];
                if (!row.IsLOGINNAMENull())
                {
                    this.txtLoginName.Text = row.LOGINNAME;
                }
                if (!row.IsEXPIRATIONTIMENull())
                {
                    this.txtExpirationTime.Text = row.EXPIRATIONTIME.ToShortDateString();
                }
                if (!row.IsIDCARDNull())
                {
                    this.txtIDCard.Text = row.IDCARD;
                }
                if (!row.IsMOBILENull())
                {
                    this.txtMobile.Text = row.MOBILE;
                }
                if (!row.IsORGNAMENull())
                {
                    this.txtOrgName.Text = row.ORGNAME;
                }
                if (!row.IsTELPHONENull())
                {
                    this.txtTelphone.Text = row.TELPHONE;
                }
                if (!row.IsUSERNAMENull())
                {
                    this.txtUserName.Text = row.USERNAME;
                }
                if (!row.IsUSERPASSWORDNull())
                {
                    this.hdPassword.Value = row.USERPASSWORD;
                }
            }
        }

        private bool AgreeLogin()
        {
            TempUser user = this.CollectTempUser();
            if (user != null && user.TEMPUSER.Count == 1)
            {
                this.m_TempUserService.AgreeLogin(user);
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool DisAgreeLogin()
        {
            TempUser user = this.CollectTempUser();
            if (user != null && user.TEMPUSER.Count == 1)
            {
                this.m_TempUserService.DisAgreeLogin(user);
                return true;
            }
            else
            {
                return false;
            }
        }

        private TempUser CollectTempUser()
        {
            TempUser rtnUser = new TempUser();
            TempUser.TEMPUSERRow userRow = rtnUser.TEMPUSER.NewTEMPUSERRow();

            userRow.TEMPUSERID = this.m_UserID;
            userRow.LOGINNAME = this.txtLoginName.Text.Trim();

            DateTime expirationTime = DateTime.MinValue;
            if (DateTime.TryParse(this.txtExpirationTime.Text.Trim(), out expirationTime))
            {
                userRow.EXPIRATIONTIME = expirationTime;
            }
            else
            {
                throw new Exception("过期时间格式不对");
            }

            userRow.IDCARD = this.txtIDCard.Text.Trim();
            userRow.MOBILE = this.txtMobile.Text.Trim();
            userRow.ORGNAME = this.txtOrgName.Text.Trim();
            userRow.TELPHONE = this.txtOrgName.Text.Trim();
            userRow.USERNAME = this.txtUserName.Text.Trim();
            userRow.EXAMEUSERID = this.m_CurrentUser.User_ID;
            userRow.USERPASSWORD = this.hdPassword.Value;

            rtnUser.TEMPUSER.AddTEMPUSERRow(userRow);
            userRow.AcceptChanges();
            userRow.SetModified();
            return rtnUser;
        }

        private void ResetPassword()
        {
            TempUser rtnUser = new TempUser();
            TempUser.TEMPUSERRow userRow = rtnUser.TEMPUSER.NewTEMPUSERRow();

            userRow.TEMPUSERID = this.m_UserID;
            userRow.LOGINNAME = this.txtLoginName.Text.Trim();

            DateTime expirationTime = DateTime.MinValue;
            if (DateTime.TryParse(this.txtExpirationTime.Text.Trim(), out expirationTime))
            {
                userRow.EXPIRATIONTIME = expirationTime;
            }
            else
            {
                throw new Exception("过期时间格式不对");
            }

            userRow.IDCARD = this.txtIDCard.Text.Trim();
            userRow.MOBILE = this.txtMobile.Text.Trim();
            userRow.ORGNAME = this.txtOrgName.Text.Trim();
            userRow.TELPHONE = this.txtOrgName.Text.Trim();
            userRow.USERNAME = this.txtUserName.Text.Trim();
            userRow.EXAMEUSERID = this.m_CurrentUser.User_ID;
            userRow.USERPASSWORD = "000000";

            if (this.m_TempUserService.ResetPassword(userRow))
            {
                Alert("重置密码成功");
            }
            else
            {
                Alert("重置密码失败");
            }
        }

        private void Jump(string message, string url)
        {
            Response.Write("<Script>alert(\"" + message + "\");window.location.href=\"" + url + "\"</Script>");
        }
        #endregion
    }
}